<!DOCTYPE html>
<html lang="zh-CN">
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <meta name="robots" content="noodp" />
        <title>ArrayList源码阅读（上） - MC&#39; blog</title><meta name="Description" content="MC&#39; Blog，不定期的技术分享"><meta property="og:title" content="ArrayList源码阅读（上）" />
<meta property="og:description" content="ArrayList源码阅读 ArrayList是使用比较多的一个List，它的底层实现是使用的是一个数组，从继承性来讲，它继承了Abstrac" />
<meta property="og:type" content="article" />
<meta property="og:url" content="http://example.org/arraylist%E6%BA%90%E7%A0%81%E9%98%85%E8%AF%BB%E4%B8%8A/" /><meta property="og:image" content="http://example.org/logo.png"/><meta property="article:section" content="posts" />
<meta property="article:published_time" content="2018-05-27T14:18:00+08:00" />
<meta property="article:modified_time" content="2018-05-27T14:18:00+08:00" /><meta property="og:site_name" content="MC&#39; Blog" />
<meta name="twitter:card" content="summary_large_image"/>
<meta name="twitter:image" content="http://example.org/logo.png"/>

<meta name="twitter:title" content="ArrayList源码阅读（上）"/>
<meta name="twitter:description" content="ArrayList源码阅读 ArrayList是使用比较多的一个List，它的底层实现是使用的是一个数组，从继承性来讲，它继承了Abstrac"/>
<meta name="application-name" content="MC&#39; Blog">
<meta name="apple-mobile-web-app-title" content="MC&#39; Blog"><meta name="theme-color" content="#ffffff"><meta name="msapplication-TileColor" content="#da532c"><link rel="shortcut icon" type="image/x-icon" href="/favicon.ico" />
        <link rel="icon" type="image/png" sizes="32x32" href="/favicon-32x32.png">
        <link rel="icon" type="image/png" sizes="16x16" href="/favicon-16x16.png"><link rel="apple-touch-icon" sizes="180x180" href="/apple-touch-icon.png"><link rel="mask-icon" href="/safari-pinned-tab.svg" color="#5bbad5"><link rel="manifest" href="/site.webmanifest"><link rel="canonical" href="http://example.org/arraylist%E6%BA%90%E7%A0%81%E9%98%85%E8%AF%BB%E4%B8%8A/" /><link rel="prev" href="http://example.org/%E9%93%BE%E8%A1%A8/" /><link rel="next" href="http://example.org/arraylist-%E6%BA%90%E7%A0%81%E9%98%85%E8%AF%BB%E4%B8%AD/" /><link rel="stylesheet" href="/css/style.min.css"><link rel="preload" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@6.1.1/css/all.min.css" as="style" onload="this.onload=null;this.rel='stylesheet'">
        <noscript><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@6.1.1/css/all.min.css"></noscript><link rel="preload" href="https://cdn.jsdelivr.net/npm/animate.css@4.1.1/animate.min.css" as="style" onload="this.onload=null;this.rel='stylesheet'">
        <noscript><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/animate.css@4.1.1/animate.min.css"></noscript><script type="application/ld+json">
    {
        "@context": "http://schema.org",
        "@type": "BlogPosting",
        "headline": "ArrayList源码阅读（上）",
        "inLanguage": "zh-CN",
        "mainEntityOfPage": {
            "@type": "WebPage",
            "@id": "http:\/\/example.org\/arraylist%E6%BA%90%E7%A0%81%E9%98%85%E8%AF%BB%E4%B8%8A\/"
        },"image": ["http:\/\/example.org\/images\/Apple-Devices-Preview.png"],"genre": "posts","keywords": "Java","wordcount":  7416 ,
        "url": "http:\/\/example.org\/arraylist%E6%BA%90%E7%A0%81%E9%98%85%E8%AF%BB%E4%B8%8A\/","datePublished": "2018-05-27T14:18:00+08:00","dateModified": "2018-05-27T14:18:00+08:00","license": "This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License.","publisher": {
            "@type": "Organization",
            "name": "xxxx","logo": {
                    "@type": "ImageObject",
                    "url": "http:\/\/example.org\/images\/avatar.png",
                    "width":  528 ,
                    "height":  560 
                }},"author": {
                "@type": "Person",
                "name": "MC"
            },"description": ""
    }
    </script></head>
    <body data-header-desktop="fixed" data-header-mobile="auto"><script type="text/javascript">(window.localStorage && localStorage.getItem('theme') ? localStorage.getItem('theme') === 'dark' : ('auto' === 'auto' ? window.matchMedia('(prefers-color-scheme: dark)').matches : 'auto' === 'dark')) && document.body.setAttribute('theme', 'dark');</script>

        <div id="mask"></div><div class="wrapper"><header class="desktop" id="header-desktop">
    <div class="header-wrapper">
        <div class="header-title">
            <a href="/" title="MC&#39; blog"><span class="header-title-pre"><i class='far fa-kiss-wink-heart fa-fw' aria-hidden='true'></i></span>MC&#39; Blog</a>
        </div>
        <div class="menu">
            <div class="menu-inner"><a class="menu-item" href="/posts/"> 所有文章 </a><a class="menu-item" href="/tags/"> 标签 </a><a class="menu-item" href="/categories/"> 分类 </a><a class="menu-item" href="/categories/documentation/"> 文档 </a><a class="menu-item" href="/about/"> 关于 </a><a class="menu-item" href="https://github.com/woxin123" title="GitHub" rel="noopener noreffer" target="_blank"><i class='fab fa-github fa-fw' aria-hidden='true'></i>  </a><span class="menu-item delimiter"></span><span class="menu-item search" id="search-desktop">
                        <input type="text" placeholder="搜索文章标题或内容..." id="search-input-desktop">
                        <a href="javascript:void(0);" class="search-button search-toggle" id="search-toggle-desktop" title="搜索">
                            <i class="fas fa-search fa-fw" aria-hidden="true"></i>
                        </a>
                        <a href="javascript:void(0);" class="search-button search-clear" id="search-clear-desktop" title="清空">
                            <i class="fas fa-times-circle fa-fw" aria-hidden="true"></i>
                        </a>
                        <span class="search-button search-loading" id="search-loading-desktop">
                            <i class="fas fa-spinner fa-fw fa-spin" aria-hidden="true"></i>
                        </span>
                    </span><a href="javascript:void(0);" class="menu-item theme-switch" title="切换主题">
                    <i class="fas fa-adjust fa-fw" aria-hidden="true"></i>
                </a></div>
        </div>
    </div>
</header><header class="mobile" id="header-mobile">
    <div class="header-container">
        <div class="header-wrapper">
            <div class="header-title">
                <a href="/" title="MC&#39; blog"><span class="header-title-pre"><i class='far fa-kiss-wink-heart fa-fw' aria-hidden='true'></i></span>MC&#39; Blog</a>
            </div>
            <div class="menu-toggle" id="menu-toggle-mobile">
                <span></span><span></span><span></span>
            </div>
        </div>
        <div class="menu" id="menu-mobile"><div class="search-wrapper">
                    <div class="search mobile" id="search-mobile">
                        <input type="text" placeholder="搜索文章标题或内容..." id="search-input-mobile">
                        <a href="javascript:void(0);" class="search-button search-toggle" id="search-toggle-mobile" title="搜索">
                            <i class="fas fa-search fa-fw" aria-hidden="true"></i>
                        </a>
                        <a href="javascript:void(0);" class="search-button search-clear" id="search-clear-mobile" title="清空">
                            <i class="fas fa-times-circle fa-fw" aria-hidden="true"></i>
                        </a>
                        <span class="search-button search-loading" id="search-loading-mobile">
                            <i class="fas fa-spinner fa-fw fa-spin" aria-hidden="true"></i>
                        </span>
                    </div>
                    <a href="javascript:void(0);" class="search-cancel" id="search-cancel-mobile">
                        取消
                    </a>
                </div><a class="menu-item" href="/posts/" title="">所有文章</a><a class="menu-item" href="/tags/" title="">标签</a><a class="menu-item" href="/categories/" title="">分类</a><a class="menu-item" href="/categories/documentation/" title="">文档</a><a class="menu-item" href="/about/" title="">关于</a><a class="menu-item" href="https://github.com/woxin123" title="GitHub" rel="noopener noreffer" target="_blank"><i class='fab fa-github fa-fw' aria-hidden='true'></i></a><a href="javascript:void(0);" class="menu-item theme-switch" title="切换主题">
                <i class="fas fa-adjust fa-fw" aria-hidden="true"></i>
            </a></div>
    </div>
</header><div class="search-dropdown desktop">
        <div id="search-dropdown-desktop"></div>
    </div>
    <div class="search-dropdown mobile">
        <div id="search-dropdown-mobile"></div>
    </div><main class="main">
                <div class="container"><div class="toc" id="toc-auto">
            <h2 class="toc-title">目录</h2>
            <div class="toc-content" id="toc-content-auto"></div>
        </div><article class="page single"><h1 class="single-title animate__animated animate__flipInX">ArrayList源码阅读（上）</h1><div class="post-meta">
            <div class="post-meta-line"><span class="post-author"><a href="/" title="Author" rel="author" class="author"><i class="fas fa-user-circle fa-fw" aria-hidden="true"></i>MC</a></span>&nbsp;<span class="post-category">收录于 <a href="/categories/java/"><i class="far fa-folder fa-fw" aria-hidden="true"></i>Java</a></span></div>
            <div class="post-meta-line"><i class="far fa-calendar-alt fa-fw" aria-hidden="true"></i>&nbsp;<time datetime="2018-05-27">2018-05-27</time>&nbsp;<i class="fas fa-pencil-alt fa-fw" aria-hidden="true"></i>&nbsp;约 7416 字&nbsp;
                <i class="far fa-clock fa-fw" aria-hidden="true"></i>&nbsp;预计阅读 15 分钟&nbsp;<span id="/arraylist%E6%BA%90%E7%A0%81%E9%98%85%E8%AF%BB%E4%B8%8A/" class="leancloud_visitors" data-flag-title="ArrayList源码阅读（上）">
                        <i class="far fa-eye fa-fw" aria-hidden="true"></i>&nbsp;<span class=leancloud-visitors-count></span>&nbsp;次阅读
                    </span>&nbsp;</div>
        </div><div class="details toc" id="toc-static"  data-kept="">
                <div class="details-summary toc-title">
                    <span>目录</span>
                    <span><i class="details-icon fas fa-angle-right" aria-hidden="true"></i></span>
                </div>
                <div class="details-content toc-content" id="toc-content-static"><nav id="TableOfContents">
  <ul>
    <li><a href="#arraylist源码阅读">ArrayList源码阅读</a>
      <ul>
        <li><a href="#arraylist中的一些变量">ArrayList中的一些变量</a></li>
        <li><a href="#arraylist的构造器">ArrayList的构造器</a></li>
        <li><a href="#arraylist中的方法">ArrayList中的方法</a>
          <ul>
            <li>
              <ul>
                <li><a href="#1-trimtosize-这个方法是用来调整缓冲区的大小的">1. trimToSize() 这个方法是用来调整缓冲区的大小的。</a></li>
                <li><a href="#2-上面我们看到了多次使用了arrayscopy方法所以这里来看一下这个方法的源码">2. 上面我们看到了多次使用了Arrays.copy()方法。所以这里来看一下这个方法的源码。</a></li>
                <li><a href="#3-adde-e添加元素的方法">3. add(E e)添加元素的方法</a></li>
              </ul>
            </li>
            <li><a href="#4-ensurecapacityinternal-明确arraylist的容量">4. ensureCapacityInternal() 明确ArrayList的容量</a>
              <ul>
                <li><a href="#4-calculatecapacity-明确arraylist的容量">4. calculateCapacity() 明确ArrayList的容量</a></li>
                <li><a href="#5-ensureexplicitcapacity">5. ensureExplicitCapacity()</a></li>
                <li><a href="#6-growint-mincapacity-这个方法时用来扩容的">6. grow(int minCapacity) 这个方法时用来扩容的</a></li>
                <li><a href="#6-toarray-这个方法就是将arraylist变成一个object数组对返回的数组操作不会影响到原arraylist">6. toArray() 这个方法就是将ArrayList变成一个Object数组，对返回的数组操作不会影响到原ArrayList.</a></li>
                <li><a href="#7-toarrayt-a">7. toArray(T[] a)</a></li>
                <li><a href="#8-addint-index-e-element">8. add(int index, E element)</a></li>
                <li><a href="#9-removeint-index">9. remove(int index)</a></li>
                <li><a href="#10-removeobject-o-删除指定的元素">10. remove(Object o) 删除指定的元素</a></li>
                <li><a href="#11-clear清空arraylist">11. clear()清空ArrayList</a></li>
                <li><a href="#12-addall">12. addAll()</a></li>
                <li><a href="#13-序列化的方法">13. 序列化的方法</a></li>
                <li><a href="#14-arraylist中还有一个clone方法">14. ArrayList中还有一个clone()方法</a></li>
              </ul>
            </li>
          </ul>
        </li>
      </ul>
    </li>
  </ul>
</nav></div>
            </div><div class="content" id="content"><h2 id="arraylist源码阅读">ArrayList源码阅读</h2>
<p>ArrayList是使用比较多的一个List，它的底层实现是使用的是一个数组，从继承性来讲，它继承了AbstractList的接口，并实现了List、RandomAccess、Cloneable和Serializable序列化接口。</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-java" data-lang="java"><span class="line"><span class="cl"><span class="kd">public</span> <span class="kd">class</span> <span class="nc">ArrayList</span><span class="o">&lt;</span><span class="n">E</span><span class="o">&gt;</span> <span class="kd">extends</span> <span class="n">AbstractList</span><span class="o">&lt;</span><span class="n">E</span><span class="o">&gt;</span> <span class="kd">implements</span> <span class="n">List</span><span class="o">&lt;</span><span class="n">E</span><span class="o">&gt;,</span> <span class="n">RandomAccess</span><span class="o">,</span> <span class="n">Cloneable</span><span class="o">,</span> <span class="n">Serializable</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl"><span class="o">}</span>
</span></span></code></pre></td></tr></table>
</div>
</div><p>下面的是ArrayList的继承结构图：</p>
<p><img
        class="lazyload"
        src="/svg/loading.min.svg"
        data-src="https://img-blog.csdn.net/20180527141209420?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3N0YXJleHBsb2Rl/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70"
        data-srcset="https://img-blog.csdn.net/20180527141209420?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3N0YXJleHBsb2Rl/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70, https://img-blog.csdn.net/20180527141209420?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3N0YXJleHBsb2Rl/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70 1.5x, https://img-blog.csdn.net/20180527141209420?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3N0YXJleHBsb2Rl/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70 2x"
        data-sizes="auto"
        alt="https://img-blog.csdn.net/20180527141209420?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3N0YXJleHBsb2Rl/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70"
        title="ArrayList继承图" /></p>
<h3 id="arraylist中的一些变量">ArrayList中的一些变量</h3>
<p>首先来看一下ArrayList中的一些变量：</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span><span class="lnt">16
</span><span class="lnt">17
</span><span class="lnt">18
</span><span class="lnt">19
</span><span class="lnt">20
</span><span class="lnt">21
</span><span class="lnt">22
</span><span class="lnt">23
</span><span class="lnt">24
</span><span class="lnt">25
</span><span class="lnt">26
</span><span class="lnt">27
</span><span class="lnt">28
</span><span class="lnt">29
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-java" data-lang="java"><span class="line"><span class="cl"><span class="c1">// 序列化ID
</span></span></span><span class="line"><span class="cl"><span class="c1"></span><span class="kd">private</span> <span class="kd">static</span> <span class="kd">final</span> <span class="kt">long</span> <span class="n">serialVersionUID</span> <span class="o">=</span> <span class="mi">8683452581122892189L</span><span class="o">;</span>
</span></span><span class="line"><span class="cl"><span class="c1">// ArryList数组的默认的容量 
</span></span></span><span class="line"><span class="cl"><span class="c1"></span><span class="kd">private</span> <span class="kd">static</span> <span class="kd">final</span> <span class="kt">int</span> <span class="n">DEFAULT_CAPACITY</span> <span class="o">=</span> <span class="mi">10</span><span class="o">;</span> 
</span></span><span class="line"><span class="cl"><span class="c1">// 一个空的数组，当用户指定容量为0时，返回该数组 
</span></span></span><span class="line"><span class="cl"><span class="c1"></span><span class="kd">private</span> <span class="kd">static</span> <span class="kd">final</span> <span class="n">Object</span><span class="o">[]</span> <span class="n">EMPTY_ELEMENTDATA</span> <span class="o">=</span> <span class="o">{};</span>
</span></span><span class="line"><span class="cl"><span class="c1">// 一个空数组的实例
</span></span></span><span class="line"><span class="cl"><span class="c1">// - 当用户没有指定ArrayList的容量时，也就是调用无参构造的时候，返回的是该数组 刚创建的ArrayList，其中的数据量为0
</span></span></span><span class="line"><span class="cl"><span class="c1">// - 当用户第一次添加元素时，该数组将会扩容，变为默认的10（DEFAULT_CAPACITY） 通过ensureCapacityInternal()
</span></span></span><span class="line"><span class="cl"><span class="c1">// 它于EMPTY_ELEMENTDATA的区别是，该数组是默认返回的，EMPTY_ELEMENTDATA是用户指定容量为0时返回的
</span></span></span><span class="line"><span class="cl"><span class="c1"></span><span class="kd">private</span> <span class="kd">static</span> <span class="kd">final</span> <span class="n">Object</span><span class="o">[]</span> <span class="n">DEFAULTCAPACITY_EMPTY_ELEMENTDATA</span> <span class="o">=</span> <span class="o">{};</span>
</span></span><span class="line"><span class="cl"><span class="c1">// ArrayList基于数组实现，该数组保存数据，ArrayList的容量就是该数组的长度 
</span></span></span><span class="line"><span class="cl"><span class="c1">// - 该值为DEFAULTCAPACITY_EMPTY_ELEMENTDATA
</span></span></span><span class="line"><span class="cl"><span class="c1">// - 当用户第一次添加元素进入ArrayList中时，该数组将扩容为DEFAULT_CAPACITY
</span></span></span><span class="line"><span class="cl"><span class="c1">// - transient 修饰element表示不序列化这个变量
</span></span></span><span class="line"><span class="cl"><span class="c1"></span><span class="kd">transient</span> <span class="n">Object</span><span class="o">[]</span> <span class="n">elementData</span><span class="o">;</span>
</span></span><span class="line"><span class="cl"><span class="c1">// ArrayList中元素的个数
</span></span></span><span class="line"><span class="cl"><span class="c1"></span><span class="kd">private</span> <span class="kt">int</span> <span class="n">size</span><span class="o">;</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"><span class="cm">/**
</span></span></span><span class="line"><span class="cl"><span class="cm"> * 数组缓冲区最大容量
</span></span></span><span class="line"><span class="cl"><span class="cm"> * The maximum size of array to allocate.
</span></span></span><span class="line"><span class="cl"><span class="cm"> * - 一些VM会在一个数组中存储某些介质 为什么减8的原因
</span></span></span><span class="line"><span class="cl"><span class="cm"> * - 尝试分配这个最大存储容量，可能会导致OutOfMemoryError(当该值 &gt; VM 限制时)
</span></span></span><span class="line"><span class="cl"><span class="cm"> * Some VMs reserve some header words in an array.
</span></span></span><span class="line"><span class="cl"><span class="cm"> * Attempts to allocate larger arrays may result in
</span></span></span><span class="line"><span class="cl"><span class="cm"> * OutOfMemoryError: Requested array size exceeds VM limit
</span></span></span><span class="line"><span class="cl"><span class="cm"> */</span>
</span></span><span class="line"><span class="cl"><span class="kd">private</span> <span class="kd">static</span> <span class="kd">final</span> <span class="kt">int</span> <span class="n">MAX_ARRAY_SIZE</span> <span class="o">=</span> <span class="n">Integer</span><span class="o">.</span><span class="na">MAX_VALUE</span> <span class="o">-</span> <span class="mi">8</span><span class="o">;</span>
</span></span></code></pre></td></tr></table>
</div>
</div><p>还有一个很重要的变量，它不在ArrayList中，它在AbstractList中，它就是<code>modCount</code>。</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-java" data-lang="java"><span class="line"><span class="cl"><span class="kd">protected</span> <span class="kd">transient</span> <span class="kt">int</span> <span class="n">modCount</span> <span class="o">=</span> <span class="mi">0</span><span class="o">;</span>
</span></span></code></pre></td></tr></table>
</div>
</div><p>这个变量同样不参与序列化，这个变量的作用是记录ArrayList的修改次数，记录它的修改次数有什么用呢？这个会在后面说明。</p>
<h3 id="arraylist的构造器">ArrayList的构造器</h3>
<p>上面也说道的一些变量它们与ArrayList的构造器相关，看了这块就会明白上面的话了。
ArrayList有三个构造器，分别是指定容量大小的，无参的和一个参数为Collection的构造器。
指定大小的构造器：</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span><span class="lnt">16
</span><span class="lnt">17
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-fallback" data-lang="fallback"><span class="line"><span class="cl">/**
</span></span><span class="line"><span class="cl"> * 创建一个初始容量的，空的ArrayList
</span></span><span class="line"><span class="cl"> * @param initialCapacity 初始容量
</span></span><span class="line"><span class="cl"> * @throws IllegalArgumentException 当初始容量为小于0时抛出
</span></span><span class="line"><span class="cl"> * is negative
</span></span><span class="line"><span class="cl"> */
</span></span><span class="line"><span class="cl">public ArrayList(int initialCapacity) {
</span></span><span class="line"><span class="cl">    if (initialCapacity &gt; 0) {
</span></span><span class="line"><span class="cl">        this.elementData = new Object[initialCapacity];
</span></span><span class="line"><span class="cl">    } else if (initialCapacity == 0) {
</span></span><span class="line"><span class="cl">        // 这里当指定的初始容量为0时返回
</span></span><span class="line"><span class="cl">        this.elementData = EMPTY_ELEMENTDATA;
</span></span><span class="line"><span class="cl">    } else {
</span></span><span class="line"><span class="cl">        throw new IllegalArgumentException(&#34;Illegal Capacity: &#34; +
</span></span><span class="line"><span class="cl">                initialCapacity);
</span></span><span class="line"><span class="cl">    }
</span></span><span class="line"><span class="cl">}
</span></span></code></pre></td></tr></table>
</div>
</div><p>无参的构造器：</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span><span class="lnt">5
</span><span class="lnt">6
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-fallback" data-lang="fallback"><span class="line"><span class="cl">/**
</span></span><span class="line"><span class="cl"> * 无参构造，返回DEFAULTCAPACITY_EMPTY_ELEMENTDATA
</span></span><span class="line"><span class="cl"> */
</span></span><span class="line"><span class="cl">public ArrayList() {
</span></span><span class="line"><span class="cl">    this.elementData = DEFAULTCAPACITY_EMPTY_ELEMENTDATA;
</span></span><span class="line"><span class="cl">}
</span></span></code></pre></td></tr></table>
</div>
</div><p>参数为Collection的构造器：</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span><span class="lnt">16
</span><span class="lnt">17
</span><span class="lnt">18
</span><span class="lnt">19
</span><span class="lnt">20
</span><span class="lnt">21
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-fallback" data-lang="fallback"><span class="line"><span class="cl">/**
</span></span><span class="line"><span class="cl"> * 创建一个包含collection的ArrayList
</span></span><span class="line"><span class="cl"> *
</span></span><span class="line"><span class="cl"> * @param c 要放入ArrayList中的集合，其内元素将全部会添加到新建的ArrayList中
</span></span><span class="line"><span class="cl"> * @throws NullPointerException 当c为空时
</span></span><span class="line"><span class="cl"> */
</span></span><span class="line"><span class="cl">public ArrayList(Collection&lt;? extends E&gt; c) {
</span></span><span class="line"><span class="cl">    // 将集合转化成数组
</span></span><span class="line"><span class="cl">    elementData = c.toArray();
</span></span><span class="line"><span class="cl">    if ((size = elementData.length) != 0) {         // 把数组的长度赋给ArrayList的size，并判断是否为空，当不等于空
</span></span><span class="line"><span class="cl">        // c.toArray might (incorrectly) not return Object[] (see 6260652)
</span></span><span class="line"><span class="cl">        // c.toArray 可能不会返回Object[] ,这是一个bug
</span></span><span class="line"><span class="cl">        if (elementData.getClass() != Object[].class)
</span></span><span class="line"><span class="cl">            // 若c.toArray()返回的数组不是Object[]，则利用Arrays.copyOf();来构造一个大小为size的Object[]
</span></span><span class="line"><span class="cl">            elementData = Arrays.copyOf(elementData, size, Object[].class);
</span></span><span class="line"><span class="cl">    } else {
</span></span><span class="line"><span class="cl">        // 替换空的数组
</span></span><span class="line"><span class="cl">        // replace with empty array.
</span></span><span class="line"><span class="cl">        this.elementData = EMPTY_ELEMENTDATA;
</span></span><span class="line"><span class="cl">    }
</span></span><span class="line"><span class="cl">}
</span></span></code></pre></td></tr></table>
</div>
</div><h3 id="arraylist中的方法">ArrayList中的方法</h3>
<h5 id="1-trimtosize-这个方法是用来调整缓冲区的大小的">1. trimToSize() 这个方法是用来调整缓冲区的大小的。</h5>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-fallback" data-lang="fallback"><span class="line"><span class="cl">/**
</span></span><span class="line"><span class="cl"> * 将数组的缓冲区调整到实际ArrayList存储元素的大小，即elementData = Arrays.copOf(elementData, size)
</span></span><span class="line"><span class="cl"> * 该方法有用户手动调用目的是为了减少空间的浪费
</span></span><span class="line"><span class="cl"> */
</span></span><span class="line"><span class="cl">public void trimToSize() {
</span></span><span class="line"><span class="cl">    modCount++; // 这个是记录ArrayList修改的次数
</span></span><span class="line"><span class="cl">    // 当实际的大小 &lt; 缓冲区大小时
</span></span><span class="line"><span class="cl">    // 如调用默认的构造函数后，刚添加第一个元素，此时它的缓冲区的大小为10， 可以调用该函数调整
</span></span><span class="line"><span class="cl">    if (size &lt; elementData.length) {
</span></span><span class="line"><span class="cl">        elementData = (size == 0)
</span></span><span class="line"><span class="cl">                ? EMPTY_ELEMENTDATA
</span></span><span class="line"><span class="cl">                : Arrays.copyOf(elementData, size);
</span></span><span class="line"><span class="cl">    }
</span></span><span class="line"><span class="cl">}
</span></span></code></pre></td></tr></table>
</div>
</div><h5 id="2-上面我们看到了多次使用了arrayscopy方法所以这里来看一下这个方法的源码">2. 上面我们看到了多次使用了Arrays.copy()方法。所以这里来看一下这个方法的源码。</h5>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span><span class="lnt">16
</span><span class="lnt">17
</span><span class="lnt">18
</span><span class="lnt">19
</span><span class="lnt">20
</span><span class="lnt">21
</span><span class="lnt">22
</span><span class="lnt">23
</span><span class="lnt">24
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-fallback" data-lang="fallback"><span class="line"><span class="cl">// 复制指定的数组，截取或用 null 填充（如有必要），以使副本具有指定的长度。
</span></span><span class="line"><span class="cl">// 对于在原数组和副本中都有效的所有索引，这两个数组将包含相同的值。
</span></span><span class="line"><span class="cl">// 对于在副本中有效而在原数组无效的所有索引，副本将包含 null。当且仅当指定长度大于原数组的长度时，
</span></span><span class="line"><span class="cl">// 这些索引存在。所得数组和原数组属于完全相同的类。
</span></span><span class="line"><span class="cl">@SuppressWarnings(&#34;unchecked&#34;)
</span></span><span class="line"><span class="cl">public static &lt;T&gt; T[] copyOf(T[] original, int newLength) {
</span></span><span class="line"><span class="cl">    // 这个方法调用了下面的这个方法
</span></span><span class="line"><span class="cl">    return (T[]) copyOf(original, newLength, original.getClass());
</span></span><span class="line"><span class="cl">}
</span></span><span class="line"><span class="cl">// 这个方法和上面的方法不同的地方就是它有一个名为newType的参数，所以它最终返回的数组类型为newType。
</span></span><span class="line"><span class="cl">public static &lt;T,U&gt; T[] copyOf(U[] original, int newLength, Class&lt;? extends T[]&gt; newType) {
</span></span><span class="line"><span class="cl">    @SuppressWarnings(&#34;unchecked&#34;) 
</span></span><span class="line"><span class="cl">    // 判断newType是不是Object[]类型的
</span></span><span class="line"><span class="cl">    T[] copy = ((Object)newType == (Object)Object[].class)
</span></span><span class="line"><span class="cl">        // 如果是直接new
</span></span><span class="line"><span class="cl">        ? (T[]) new Object[newLength]
</span></span><span class="line"><span class="cl">        // 如果不是，通过调用这个方法获取，newType.getComponentType()获取数组组件类型的Class
</span></span><span class="line"><span class="cl">        // 这个在往下就到native层了
</span></span><span class="line"><span class="cl">        : (T[]) Array.newInstance(newType.getComponentType(), newLength);
</span></span><span class="line"><span class="cl">    // 下面的这个方法是一个native层的一个方法，在后面也用的比较多，这个是用来实现数组复制的
</span></span><span class="line"><span class="cl">    System.arraycopy(original, 0, copy, 0,
</span></span><span class="line"><span class="cl">                     Math.min(original.length, newLength));
</span></span><span class="line"><span class="cl">    return copy;
</span></span><span class="line"><span class="cl">}
</span></span></code></pre></td></tr></table>
</div>
</div><p><code>System.arraycopy()</code>的原型如下：</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-fallback" data-lang="fallback"><span class="line"><span class="cl">public static native void arraycopy(Object src, int srcPos,
</span></span><span class="line"><span class="cl">                                        Object dest, int destPos,
</span></span><span class="line"><span class="cl">                                        int length);
</span></span></code></pre></td></tr></table>
</div>
</div><p>它的四个参数的作用如下：</p>
<table>
<thead>
<tr>
<th style="text-align:left">参数</th>
<th style="text-align:left">作用</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:left">src</td>
<td style="text-align:left">原数组，也就是需要复制的数组</td>
</tr>
<tr>
<td style="text-align:left">srcPos</td>
<td style="text-align:left">原数组的复制的起始位置</td>
</tr>
<tr>
<td style="text-align:left">dest</td>
<td style="text-align:left">目标数组</td>
</tr>
<tr>
<td style="text-align:left">destPos</td>
<td style="text-align:left">目标数组中的起始位置</td>
</tr>
<tr>
<td style="text-align:left">length</td>
<td style="text-align:left">复制的长度</td>
</tr>
</tbody>
</table>
<p>下面的是Array.copyOf()和System.arraycopy()方法的简单使用：</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span><span class="lnt">16
</span><span class="lnt">17
</span><span class="lnt">18
</span><span class="lnt">19
</span><span class="lnt">20
</span><span class="lnt">21
</span><span class="lnt">22
</span><span class="lnt">23
</span><span class="lnt">24
</span><span class="lnt">25
</span><span class="lnt">26
</span><span class="lnt">27
</span><span class="lnt">28
</span><span class="lnt">29
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-fallback" data-lang="fallback"><span class="line"><span class="cl">import java.util.Arrays;
</span></span><span class="line"><span class="cl">public class ArrayTest {
</span></span><span class="line"><span class="cl">    public static void main(String[] args) {
</span></span><span class="line"><span class="cl">        // Array.copyOf()的用法
</span></span><span class="line"><span class="cl">        System.out.println(&#34;-------------Arrays.copyOf()的用法示例-------------&#34;);
</span></span><span class="line"><span class="cl">        Object[] array = new Object[1];
</span></span><span class="line"><span class="cl">        array[0] = &#34;Hello&#34;;
</span></span><span class="line"><span class="cl">        array = Arrays.copyOf(array, 3);
</span></span><span class="line"><span class="cl">        for(Object o : array) {
</span></span><span class="line"><span class="cl">            System.out.print((String)o);
</span></span><span class="line"><span class="cl">        }
</span></span><span class="line"><span class="cl">        System.out.println();
</span></span><span class="line"><span class="cl">        array[1] = &#34; &#34;;
</span></span><span class="line"><span class="cl">        array[2] = &#34;World!&#34;;
</span></span><span class="line"><span class="cl">        for(Object o : array) {
</span></span><span class="line"><span class="cl">            System.out.print((String)o);
</span></span><span class="line"><span class="cl">        }
</span></span><span class="line"><span class="cl">        System.out.println(&#34;-------------System.arraycopy()的用法示例---------------&#34;);
</span></span><span class="line"><span class="cl">        int[] arraySrc = new int[9];
</span></span><span class="line"><span class="cl">        for (int i = 0; i &lt; 9; i++) {
</span></span><span class="line"><span class="cl">            arraySrc[i] = i;
</span></span><span class="line"><span class="cl">        }
</span></span><span class="line"><span class="cl">        int[] arrayDest = new int[10];
</span></span><span class="line"><span class="cl">        System.arraycopy(arraySrc, 0, arrayDest, 0, 9);
</span></span><span class="line"><span class="cl">        for (int i = 0; i &lt; 10; i++) {
</span></span><span class="line"><span class="cl">            System.out.println(arrayDest[i]);
</span></span><span class="line"><span class="cl">        }
</span></span><span class="line"><span class="cl">    }
</span></span><span class="line"><span class="cl">}
</span></span></code></pre></td></tr></table>
</div>
</div><h5 id="3-adde-e添加元素的方法">3. add(E e)添加元素的方法</h5>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-fallback" data-lang="fallback"><span class="line"><span class="cl">/**
</span></span><span class="line"><span class="cl"> * Appends the specified element to the end of this list.
</span></span><span class="line"><span class="cl"> * 将指定的元素添加到ArrayList最后的位置
</span></span><span class="line"><span class="cl"> *
</span></span><span class="line"><span class="cl"> * @param e element to be appended to this list
</span></span><span class="line"><span class="cl"> * @return &lt;tt&gt;true&lt;/tt&gt; (as specified by {@link Collection#add})
</span></span><span class="line"><span class="cl"> */
</span></span><span class="line"><span class="cl">public boolean add(E e) {
</span></span><span class="line"><span class="cl">    // 确保ArrayList的容量
</span></span><span class="line"><span class="cl">    // 保证要存多少个元素，就分配多少的空间
</span></span><span class="line"><span class="cl">    ensureCapacityInternal(size + 1); // Increments modCount!!
</span></span><span class="line"><span class="cl">    elementData[size++] = e;
</span></span><span class="line"><span class="cl">    return true;
</span></span><span class="line"><span class="cl">}
</span></span></code></pre></td></tr></table>
</div>
</div><h4 id="4-ensurecapacityinternal-明确arraylist的容量">4. ensureCapacityInternal() 明确ArrayList的容量</h4>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span><span class="lnt">5
</span><span class="lnt">6
</span><span class="lnt">7
</span><span class="lnt">8
</span><span class="lnt">9
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-fallback" data-lang="fallback"><span class="line"><span class="cl">/**
</span></span><span class="line"><span class="cl"> * 私有的方法：明确ArrayList的容量
</span></span><span class="line"><span class="cl"> * 用于内部优化，保证空间资源不浪费：尤其在add()方法中
</span></span><span class="line"><span class="cl"> *
</span></span><span class="line"><span class="cl"> * @param minCapacity 指定最小容量
</span></span><span class="line"><span class="cl"> */
</span></span><span class="line"><span class="cl">private void ensureCapacityInternal(int minCapacity) {
</span></span><span class="line"><span class="cl">    ensureExplicitCapacity(calculateCapacity(elementData, minCapacity));
</span></span><span class="line"><span class="cl">}
</span></span></code></pre></td></tr></table>
</div>
</div><p>这个方法调用了calculateCapacity()方法，这个方法时一个私有的静态方法。</p>
<h5 id="4-calculatecapacity-明确arraylist的容量">4. calculateCapacity() 明确ArrayList的容量</h5>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span><span class="lnt">16
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-fallback" data-lang="fallback"><span class="line"><span class="cl">/**
</span></span><span class="line"><span class="cl"> * 私有的静态方法：明确ArrayList的容量
</span></span><span class="line"><span class="cl"> *
</span></span><span class="line"><span class="cl"> * @param elementData
</span></span><span class="line"><span class="cl"> * @param minCapacity
</span></span><span class="line"><span class="cl"> * @return 返回的是经过计算后的ArrayList的容量
</span></span><span class="line"><span class="cl"> */
</span></span><span class="line"><span class="cl">private static int calculateCapacity(Object[] elementData, int minCapacity) {
</span></span><span class="line"><span class="cl">    // 如果elementData为空的话，返回默认用量和minCapacity中的较大者
</span></span><span class="line"><span class="cl">    if (elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA) {
</span></span><span class="line"><span class="cl">        // 比较指定容量和默认容量那个大，返回较大的
</span></span><span class="line"><span class="cl">        return Math.max(DEFAULT_CAPACITY, minCapacity);
</span></span><span class="line"><span class="cl">    }
</span></span><span class="line"><span class="cl">    // 如果不为空，返回minCapacity
</span></span><span class="line"><span class="cl">    return minCapacity;
</span></span><span class="line"><span class="cl">}
</span></span></code></pre></td></tr></table>
</div>
</div><h5 id="5-ensureexplicitcapacity">5. ensureExplicitCapacity()</h5>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-fallback" data-lang="fallback"><span class="line"><span class="cl">/**
</span></span><span class="line"><span class="cl"> * 私有的方法：明确ArrayList的容量
</span></span><span class="line"><span class="cl"> * 用户内部优化，保证空间资源不浪费：尤其在add()方法中
</span></span><span class="line"><span class="cl"> * 防止溢出
</span></span><span class="line"><span class="cl"> * @param minCapacity
</span></span><span class="line"><span class="cl"> */
</span></span><span class="line"><span class="cl">private void ensureExplicitCapacity(int minCapacity) {
</span></span><span class="line"><span class="cl">    // 将修改的统计次数加1
</span></span><span class="line"><span class="cl">    modCount++;
</span></span><span class="line"><span class="cl">    // overflow-conscious code
</span></span><span class="line"><span class="cl">    // 防止溢出，确保最小容量 &gt; 数组缓冲区的容量
</span></span><span class="line"><span class="cl">    if (minCapacity - elementData.length &gt; 0)
</span></span><span class="line"><span class="cl">        grow(minCapacity);
</span></span><span class="line"><span class="cl">}
</span></span></code></pre></td></tr></table>
</div>
</div><h5 id="6-growint-mincapacity-这个方法时用来扩容的">6. grow(int minCapacity) 这个方法时用来扩容的</h5>
<p>这个是ArrayList中一个比较重要的内容：数组扩容。</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span><span class="lnt">16
</span><span class="lnt">17
</span><span class="lnt">18
</span><span class="lnt">19
</span><span class="lnt">20
</span><span class="lnt">21
</span><span class="lnt">22
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-fallback" data-lang="fallback"><span class="line"><span class="cl">/**
</span></span><span class="line"><span class="cl"> * 私有方法：扩容，以确保能存储minCapacity个元素
</span></span><span class="line"><span class="cl"> * - 扩容计算：newCapacity = oldCapacity + (oldCapacity &gt;&gt; 1); 扩容是当前容量的1.5倍
</span></span><span class="line"><span class="cl"> * Increases the capacity to ensure that it can hold at least the
</span></span><span class="line"><span class="cl"> * number of elements specified by the minimum capacity argument.
</span></span><span class="line"><span class="cl"> *
</span></span><span class="line"><span class="cl"> * @param minCapacity the desired minimum capacity 指定最小容量
</span></span><span class="line"><span class="cl"> */
</span></span><span class="line"><span class="cl">private void grow(int minCapacity) {
</span></span><span class="line"><span class="cl">    // overflow-conscious code
</span></span><span class="line"><span class="cl">    int oldCapacity = elementData.length;
</span></span><span class="line"><span class="cl">    // 扩容的策略是newCapacity =  oldCapacity oldCapacity / 2
</span></span><span class="line"><span class="cl">    int newCapacity = oldCapacity + (oldCapacity &gt;&gt; 1);
</span></span><span class="line"><span class="cl">     // 如果使用这种策咯扩容之后比最小容量小，那么让newCapacity = minCapacity
</span></span><span class="line"><span class="cl">    if (newCapacity - minCapacity &lt; 0)
</span></span><span class="line"><span class="cl">        newCapacity = minCapacity;
</span></span><span class="line"><span class="cl">    // 也就是newCapacity &gt; MAX_ARRAY_SIZE 也就是新的容量比最大的容量还要大
</span></span><span class="line"><span class="cl">    if (newCapacity - MAX_ARRAY_SIZE &gt; 0)
</span></span><span class="line"><span class="cl">        newCapacity = hugeCapacity(minCapacity);    // 其实这个方法当minCapacity &gt;MAX_ARRAY_SIZE 时，返回Integer.MAX_VALUE
</span></span><span class="line"><span class="cl">    // minCapacity is usually close to size, so this is a win: 这里使用了Arrays.copyOf()这个方法进行扩容
</span></span><span class="line"><span class="cl">    elementData = Arrays.copyOf(elementData, newCapacity);
</span></span><span class="line"><span class="cl">}
</span></span></code></pre></td></tr></table>
</div>
</div><p>下面的就是hugeCapacity(int minCapacity)的源码。</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-fallback" data-lang="fallback"><span class="line"><span class="cl">/**
</span></span><span class="line"><span class="cl"> * 私有方法：最大容量分配，最大分配Integer.MAX_VALUE
</span></span><span class="line"><span class="cl"> *
</span></span><span class="line"><span class="cl"> * @param minCapacity
</span></span><span class="line"><span class="cl"> * @return
</span></span><span class="line"><span class="cl"> */
</span></span><span class="line"><span class="cl">private static int hugeCapacity(int minCapacity) {
</span></span><span class="line"><span class="cl">    // 小于0，抛出异常 OutOfMemoryError
</span></span><span class="line"><span class="cl">    if (minCapacity &lt; 0) // overflow
</span></span><span class="line"><span class="cl">        throw new OutOfMemoryError();
</span></span><span class="line"><span class="cl">    // 指定最小的容量大于最大能分配的容量，返回Integer.MAX_VALUE，否则返回能分配的最大值
</span></span><span class="line"><span class="cl">    return (minCapacity &gt; MAX_ARRAY_SIZE) ?
</span></span><span class="line"><span class="cl">            Integer.MAX_VALUE :
</span></span><span class="line"><span class="cl">            MAX_ARRAY_SIZE;
</span></span><span class="line"><span class="cl">}
</span></span></code></pre></td></tr></table>
</div>
</div><h5 id="6-toarray-这个方法就是将arraylist变成一个object数组对返回的数组操作不会影响到原arraylist">6. toArray() 这个方法就是将ArrayList变成一个Object数组，对返回的数组操作不会影响到原ArrayList.</h5>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-fallback" data-lang="fallback"><span class="line"><span class="cl">/**
</span></span><span class="line"><span class="cl"> * 将该数组转化成Object数组
</span></span><span class="line"><span class="cl"> * - 包含ArrayList的所有元素
</span></span><span class="line"><span class="cl"> * - 对返回的数组操作，不会影响ArrayList
</span></span><span class="line"><span class="cl"> * - 元素的存储与ArrayList一致
</span></span><span class="line"><span class="cl"> * @return an array containing all of the elements in this list in 返回一个包含ArrayList元素的数组
</span></span><span class="line"><span class="cl"> * proper sequence
</span></span><span class="line"><span class="cl"> */
</span></span><span class="line"><span class="cl">public Object[] toArray() {
</span></span><span class="line"><span class="cl">    return Arrays.copyOf(elementData, size);
</span></span><span class="line"><span class="cl">}
</span></span></code></pre></td></tr></table>
</div>
</div><p>这里可以看到这个方法就是用了Arrays.copy()方法。</p>
<h5 id="7-toarrayt-a">7. toArray(T[] a)</h5>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-fallback" data-lang="fallback"><span class="line"><span class="cl">/**
</span></span><span class="line"><span class="cl"> * 返回ArrayList元素组成的数组
</span></span><span class="line"><span class="cl"> */
</span></span><span class="line"><span class="cl">@SuppressWarnings(&#34;unchecked&#34;)
</span></span><span class="line"><span class="cl">public &lt;T&gt; T[] toArray(T[] a) {
</span></span><span class="line"><span class="cl">    // 若a.length &lt; size时新建一个T[]数组
</span></span><span class="line"><span class="cl">    if (a.length &lt; size)
</span></span><span class="line"><span class="cl">        // Make a new array of a&#39;s runtime type, but my contents:
</span></span><span class="line"><span class="cl">        return (T[]) Arrays.copyOf(elementData, size, a.getClass());
</span></span><span class="line"><span class="cl">    // 若数组a的大小 &gt;= ArrayList中元素的个数，则将ArrayList中的元素全部拷贝到a数组中
</span></span><span class="line"><span class="cl">    System.arraycopy(elementData, 0, a, 0, size);
</span></span><span class="line"><span class="cl">    if (a.length &gt; size)
</span></span><span class="line"><span class="cl">        a[size] = null;
</span></span><span class="line"><span class="cl">    return a;
</span></span><span class="line"><span class="cl">}
</span></span></code></pre></td></tr></table>
</div>
</div><p>这个方法可能看一些JDK的解释也看的稀里糊涂的（我就是这样）。
通过看它的源码就会发现，它有两种情况：
1. 数组的长度小于ArrayList的size时(<code>a.length &lt; size</code>)，调用之前介绍的<code>Arrays.copyOf()</code>方法复制elementData中的元素（也就是复制ArrayList中的元素）。
2. 数组的长度大于等于ArrayList的size，将elementData中的元素从0开始赋值给a。然后将a[size]赋值为null。
下面的是它的一个测试示例：</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span><span class="lnt">16
</span><span class="lnt">17
</span><span class="lnt">18
</span><span class="lnt">19
</span><span class="lnt">20
</span><span class="lnt">21
</span><span class="lnt">22
</span><span class="lnt">23
</span><span class="lnt">24
</span><span class="lnt">25
</span><span class="lnt">26
</span><span class="lnt">27
</span><span class="lnt">28
</span><span class="lnt">29
</span><span class="lnt">30
</span><span class="lnt">31
</span><span class="lnt">32
</span><span class="lnt">33
</span><span class="lnt">34
</span><span class="lnt">35
</span><span class="lnt">36
</span><span class="lnt">37
</span><span class="lnt">38
</span><span class="lnt">39
</span><span class="lnt">40
</span><span class="lnt">41
</span><span class="lnt">42
</span><span class="lnt">43
</span><span class="lnt">44
</span><span class="lnt">45
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-fallback" data-lang="fallback"><span class="line"><span class="cl">import java.util.*;
</span></span><span class="line"><span class="cl">public class ArrayListToArray {
</span></span><span class="line"><span class="cl">    public static void main(String[] args) {
</span></span><span class="line"><span class="cl">        System.out.println(&#34;---------第一种情况-----------&#34;);
</span></span><span class="line"><span class="cl">        String[] array1 = {&#34;Hello&#34;, &#34; &#34;, &#34;World!&#34;};
</span></span><span class="line"><span class="cl">        ArrayList&lt;String&gt; list1 = new ArrayList();
</span></span><span class="line"><span class="cl">        list1.add(&#34;my&#34;);
</span></span><span class="line"><span class="cl">        list1.add(&#34;name&#34;);
</span></span><span class="line"><span class="cl">        list1.add(&#34;is&#34;);
</span></span><span class="line"><span class="cl">        list1.add(&#34;emmmmm&#34;);
</span></span><span class="line"><span class="cl">        String[] arrayReturn1 = list1.toArray(array1);
</span></span><span class="line"><span class="cl">        System.out.println(&#34;array数组：&#34;);
</span></span><span class="line"><span class="cl">        for (String str : array1) {
</span></span><span class="line"><span class="cl">            System.out.println(str);
</span></span><span class="line"><span class="cl">        }
</span></span><span class="line"><span class="cl">        System.out.println(&#34;list数组：&#34;);
</span></span><span class="line"><span class="cl">        for (String str : list1) {
</span></span><span class="line"><span class="cl">            System.out.println(str);
</span></span><span class="line"><span class="cl">        }
</span></span><span class="line"><span class="cl">        System.out.println(&#34;arrayReturn数组：&#34;);
</span></span><span class="line"><span class="cl">        for (String str : arrayReturn1) {
</span></span><span class="line"><span class="cl">            System.out.println(str);
</span></span><span class="line"><span class="cl">        }
</span></span><span class="line"><span class="cl">        System.out.println(&#34;---------第二种情况-----------&#34;);
</span></span><span class="line"><span class="cl">        String[] array2 = {&#34;Hello&#34;, &#34; &#34;, &#34;World!&#34;, &#34;Hello&#34;, &#34; &#34;, &#34;ArrayList&#34;, &#34;!&#34;};
</span></span><span class="line"><span class="cl">        ArrayList&lt;String&gt; list2 = new ArrayList();
</span></span><span class="line"><span class="cl">        list2.add(&#34;my&#34;);
</span></span><span class="line"><span class="cl">        list2.add(&#34;name&#34;);
</span></span><span class="line"><span class="cl">        list2.add(&#34;is&#34;);
</span></span><span class="line"><span class="cl">        list2.add(&#34;emmmmm&#34;);
</span></span><span class="line"><span class="cl">        String[] arrayReturn2 = list2.toArray(array2);
</span></span><span class="line"><span class="cl">        System.out.println(&#34;array数组：&#34;);
</span></span><span class="line"><span class="cl">        for (String str : array2) {
</span></span><span class="line"><span class="cl">            System.out.println(str);
</span></span><span class="line"><span class="cl">        }
</span></span><span class="line"><span class="cl">        System.out.println(&#34;list数组：&#34;);
</span></span><span class="line"><span class="cl">        for (String str : list2) {
</span></span><span class="line"><span class="cl">            System.out.println(str);
</span></span><span class="line"><span class="cl">        }
</span></span><span class="line"><span class="cl">        System.out.println(&#34;arrayReturn数组：&#34;);
</span></span><span class="line"><span class="cl">        for (String str : arrayReturn2) {
</span></span><span class="line"><span class="cl">            System.out.println(str);
</span></span><span class="line"><span class="cl">        }
</span></span><span class="line"><span class="cl">    }
</span></span><span class="line"><span class="cl">}
</span></span></code></pre></td></tr></table>
</div>
</div><h5 id="8-addint-index-e-element">8. add(int index, E element)</h5>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span><span class="lnt">16
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-fallback" data-lang="fallback"><span class="line"><span class="cl">/**
</span></span><span class="line"><span class="cl"> * 在这个ArrayList中指定位置插入指定的元素
</span></span><span class="line"><span class="cl"> * - 在指定位置之前插入新的元素，原先的index位置往后移动一位
</span></span><span class="line"><span class="cl"> * @param index index at which the specified element is to be inserted
</span></span><span class="line"><span class="cl"> * @param element element to be inserted
</span></span><span class="line"><span class="cl"> * @throws IndexOutOfBoundsException {@inheritDoc}
</span></span><span class="line"><span class="cl"> */
</span></span><span class="line"><span class="cl">public void add(int index, E element) {
</span></span><span class="line"><span class="cl">    rangeCheckForAdd(index); // 检查是否越界
</span></span><span class="line"><span class="cl">    ensureCapacityInternal(size + 1); // Increments modCount!!这个方法和上面的那个方法的作用相同
</span></span><span class="line"><span class="cl">    // 第一个是要复制的数组，第二个是开始位置，第二个复制到哪，第四个是复制的长度
</span></span><span class="line"><span class="cl">    System.arraycopy(elementData, index, elementData, index + 1,
</span></span><span class="line"><span class="cl">            size - index);
</span></span><span class="line"><span class="cl">    elementData[index] = element;
</span></span><span class="line"><span class="cl">    size++;
</span></span><span class="line"><span class="cl">}
</span></span></code></pre></td></tr></table>
</div>
</div><p>这里有一个越界检查的方法，这个方法很简单，是一个为add提供的范围检查的方法，若果越界则抛出IndexOutOfBoundsException</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span><span class="lnt">5
</span><span class="lnt">6
</span><span class="lnt">7
</span><span class="lnt">8
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-fallback" data-lang="fallback"><span class="line"><span class="cl">/**
</span></span><span class="line"><span class="cl"> * 范围检查用于add和addAll
</span></span><span class="line"><span class="cl"> * A version of rangeCheck used by add and addAll.
</span></span><span class="line"><span class="cl"> */
</span></span><span class="line"><span class="cl">private void rangeCheckForAdd(int index) {
</span></span><span class="line"><span class="cl">    if (index &gt; size || index &lt; 0)
</span></span><span class="line"><span class="cl">        throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
</span></span><span class="line"><span class="cl">}
</span></span></code></pre></td></tr></table>
</div>
</div><p>然后这个方法还调用一个构建越界信息的方法，在这里不得不说：真的是把复用做到了极致。</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-fallback" data-lang="fallback"><span class="line"><span class="cl">private String outOfBoundsMsg(int index) {
</span></span><span class="line"><span class="cl">    return &#34;Index: &#34; + index + &#34;, Size: &#34; + size;
</span></span><span class="line"><span class="cl">}
</span></span></code></pre></td></tr></table>
</div>
</div><h5 id="9-removeint-index">9. remove(int index)</h5>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span><span class="lnt">16
</span><span class="lnt">17
</span><span class="lnt">18
</span><span class="lnt">19
</span><span class="lnt">20
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-fallback" data-lang="fallback"><span class="line"><span class="cl">/**
</span></span><span class="line"><span class="cl"> * 删除指定位置的元素
</span></span><span class="line"><span class="cl"> * index后的索引元素一次左移一位
</span></span><span class="line"><span class="cl"> * @param index the index of the element to be removed index指定位置
</span></span><span class="line"><span class="cl"> * @return the element that was removed from the list 被移出的元素
</span></span><span class="line"><span class="cl"> * @throws IndexOutOfBoundsException {@inheritDoc}
</span></span><span class="line"><span class="cl"> */
</span></span><span class="line"><span class="cl">public E remove(int index) {
</span></span><span class="line"><span class="cl">    // 检查index越界
</span></span><span class="line"><span class="cl">    rangeCheck(index);
</span></span><span class="line"><span class="cl">    // 修改次数加一
</span></span><span class="line"><span class="cl">    modCount++;
</span></span><span class="line"><span class="cl">    E oldValue = elementData(index);
</span></span><span class="line"><span class="cl">    int numMoved = size - index - 1; // 要移动的长度
</span></span><span class="line"><span class="cl">    if (numMoved &gt; 0)
</span></span><span class="line"><span class="cl">        System.arraycopy(elementData, index + 1, elementData, index,
</span></span><span class="line"><span class="cl">                numMoved);
</span></span><span class="line"><span class="cl">    // 将最后一个元素置空
</span></span><span class="line"><span class="cl">    elementData[--size] = null; // clear to let GC do its work
</span></span><span class="line"><span class="cl">    return oldValue;
</span></span></code></pre></td></tr></table>
</div>
</div><p>这个方法的源码还是很好懂的，ArrayList中有关数组复制的几乎都是用的System.arraycopy()这个方法。</p>
<h5 id="10-removeobject-o-删除指定的元素">10. remove(Object o) 删除指定的元素</h5>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span><span class="lnt">16
</span><span class="lnt">17
</span><span class="lnt">18
</span><span class="lnt">19
</span><span class="lnt">20
</span><span class="lnt">21
</span><span class="lnt">22
</span><span class="lnt">23
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-fallback" data-lang="fallback"><span class="line"><span class="cl">/**
</span></span><span class="line"><span class="cl"> * 移出list中指定的第一个元素（符合条件索引的最低值）
</span></span><span class="line"><span class="cl"> * 如果ArrayList中不包含这个元素，那么ArrayList将不会被改变
</span></span><span class="line"><span class="cl"> * @param o element to be removed from this list, if present
</span></span><span class="line"><span class="cl"> * @return &lt;tt&gt;true&lt;/tt&gt; if this list contained the specified element
</span></span><span class="line"><span class="cl"> */
</span></span><span class="line"><span class="cl">public boolean remove(Object o) {
</span></span><span class="line"><span class="cl">    // 如过o = null
</span></span><span class="line"><span class="cl">    if (o == null) { // 遍历找到null
</span></span><span class="line"><span class="cl">        for (int index = 0; index &lt; size; index++)
</span></span><span class="line"><span class="cl">            if (elementData[index] == null) {
</span></span><span class="line"><span class="cl">                fastRemove(index);
</span></span><span class="line"><span class="cl">                return true;
</span></span><span class="line"><span class="cl">            }
</span></span><span class="line"><span class="cl">    } else {
</span></span><span class="line"><span class="cl">        for (int index = 0; index &lt; size; index++)
</span></span><span class="line"><span class="cl">            if (o.equals(elementData[index])) {
</span></span><span class="line"><span class="cl">                fastRemove(index);
</span></span><span class="line"><span class="cl">                return true;
</span></span><span class="line"><span class="cl">            }
</span></span><span class="line"><span class="cl">    }
</span></span><span class="line"><span class="cl">    return false;
</span></span><span class="line"><span class="cl">}
</span></span></code></pre></td></tr></table>
</div>
</div><p>因为ArrayList集合中的元素可以是null，所以如果传入的是null，那么首先它会去寻找第一个为null的元素。
如果不是的话，那么他会去使用元素的equals方法去判断是否相等。这里也就是我们有时候要重写类的equals方法的原因了。
上面的方法中调用了一个fastRemove(index)，这个方法是ArrayList的一个私有的方法。
它的源码如下：</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-fallback" data-lang="fallback"><span class="line"><span class="cl">/*
</span></span><span class="line"><span class="cl"> * 快速删除第index个元素
</span></span><span class="line"><span class="cl"> * Private remove method that skips bounds checking and does not
</span></span><span class="line"><span class="cl"> * return the value removed.
</span></span><span class="line"><span class="cl"> */
</span></span><span class="line"><span class="cl">private void fastRemove(int index) {
</span></span><span class="line"><span class="cl">    modCount++;
</span></span><span class="line"><span class="cl">    int numMoved = size - index - 1;
</span></span><span class="line"><span class="cl">    if (numMoved &gt; 0)
</span></span><span class="line"><span class="cl">        System.arraycopy(elementData, index + 1, elementData, index,
</span></span><span class="line"><span class="cl">                numMoved);
</span></span><span class="line"><span class="cl">    elementData[--size] = null; // clear to let GC do its work 将最后一个元素清除
</span></span><span class="line"><span class="cl">}
</span></span></code></pre></td></tr></table>
</div>
</div><h5 id="11-clear清空arraylist">11. clear()清空ArrayList</h5>
<p>这个方法的作用是将ArrayList的元素全部清空。</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-fallback" data-lang="fallback"><span class="line"><span class="cl">/**
</span></span><span class="line"><span class="cl"> * 删除该list中的所有元素
</span></span><span class="line"><span class="cl"> * - 他会将数组缓冲区所有元素置为null
</span></span><span class="line"><span class="cl"> * - 清除之后，打印list会出现[], 而不是null, 这是因为toSting()和迭代器处理了
</span></span><span class="line"><span class="cl"> * Removes all of the elements from this list. The list will
</span></span><span class="line"><span class="cl"> * be empty after this call returns.
</span></span><span class="line"><span class="cl"> */
</span></span><span class="line"><span class="cl">public void clear() {
</span></span><span class="line"><span class="cl">    modCount++;
</span></span><span class="line"><span class="cl">    // clear to let GC do its work 这里把他们去不置为空，让GC工作
</span></span><span class="line"><span class="cl">    for (int i = 0; i &lt; size; i++)
</span></span><span class="line"><span class="cl">        elementData[i] = null;
</span></span><span class="line"><span class="cl">    size = 0;
</span></span><span class="line"><span class="cl">}
</span></span></code></pre></td></tr></table>
</div>
</div><h5 id="12-addall">12. addAll()</h5>
<p>addAll(Collection&lt;? extends E&gt; c)
这个方法的作用是将c集合中的元素元素添加在ArrayList的末尾</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span><span class="lnt">16
</span><span class="lnt">17
</span><span class="lnt">18
</span><span class="lnt">19
</span><span class="lnt">20
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-fallback" data-lang="fallback"><span class="line"><span class="cl">/**
</span></span><span class="line"><span class="cl"> * 将一个集合中的所有元素追加到list的末尾
</span></span><span class="line"><span class="cl"> * - ArrayList是线程不安全的，当一个线程正在将c中的元素添加到list中，
</span></span><span class="line"><span class="cl"> * 但同时另一个线程在更改c中的元素，可能会产生问题
</span></span><span class="line"><span class="cl"> * @param c collection containing elements to be added to this list 要追加的集合
</span></span><span class="line"><span class="cl"> * @return &lt;tt&gt;true&lt;/tt&gt; if this list changed as a result of the call
</span></span><span class="line"><span class="cl"> * @throws NullPointerException if the specified collection is null
</span></span><span class="line"><span class="cl"> */
</span></span><span class="line"><span class="cl">public boolean addAll(Collection&lt;? extends E&gt; c) {
</span></span><span class="line"><span class="cl">    // 将c转化为一个object数组
</span></span><span class="line"><span class="cl">    Object[] a = c.toArray();
</span></span><span class="line"><span class="cl">    // 要追加的元素的个数
</span></span><span class="line"><span class="cl">    int numNew = a.length;
</span></span><span class="line"><span class="cl">    // 扩容
</span></span><span class="line"><span class="cl">    ensureCapacityInternal(size + numNew);   // Increments modCount
</span></span><span class="line"><span class="cl">    // 复制
</span></span><span class="line"><span class="cl">    System.arraycopy(a, 0, elementData, size, numNew);
</span></span><span class="line"><span class="cl">    size += numNew;
</span></span><span class="line"><span class="cl">    return numNew != 0;
</span></span><span class="line"><span class="cl">}
</span></span></code></pre></td></tr></table>
</div>
</div><h5 id="13-序列化的方法">13. 序列化的方法</h5>
<p>上面提到了elementData和size不参与序列化，但是平常使用的时候ArrayList是可以正常的序列化的，因为它有自己的序列化和反序列化的方法。
序列化方法writeObject()</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span><span class="lnt">16
</span><span class="lnt">17
</span><span class="lnt">18
</span><span class="lnt">19
</span><span class="lnt">20
</span><span class="lnt">21
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-fallback" data-lang="fallback"><span class="line"><span class="cl">/**
</span></span><span class="line"><span class="cl"> * 私有方法
</span></span><span class="line"><span class="cl"> * 将ArrayList实例序列化
</span></span><span class="line"><span class="cl"> */
</span></span><span class="line"><span class="cl">private void writeObject(java.io.ObjectOutputStream s)
</span></span><span class="line"><span class="cl">        throws java.io.IOException {
</span></span><span class="line"><span class="cl">    // Write out element count, and any hidden stuff
</span></span><span class="line"><span class="cl">    // 写入那些没有被序列化的属性
</span></span><span class="line"><span class="cl">    int expectedModCount = modCount;
</span></span><span class="line"><span class="cl">    s.defaultWriteObject();
</span></span><span class="line"><span class="cl">    // Write out size as capacity for behavioural compatibility with clone()
</span></span><span class="line"><span class="cl">    s.writeInt(size);
</span></span><span class="line"><span class="cl">    // Write out all elements in the proper order.
</span></span><span class="line"><span class="cl">    // 以合适的顺序写入所有的元素
</span></span><span class="line"><span class="cl">    for (int i = 0; i &lt; size; i++) {
</span></span><span class="line"><span class="cl">        s.writeObject(elementData[i]);
</span></span><span class="line"><span class="cl">    }
</span></span><span class="line"><span class="cl">    if (modCount != expectedModCount) {
</span></span><span class="line"><span class="cl">        throw new ConcurrentModificationException();
</span></span><span class="line"><span class="cl">    }
</span></span><span class="line"><span class="cl">}
</span></span></code></pre></td></tr></table>
</div>
</div><p>这里就体现出modCount的作用了，如果正在序列化的时候ArrayList被修改了，那么modCount的值肯定会变大，如果序列化结束后modCount的大小和之前记录的大小不一样大，那么肯定ArrayList在序列化的过程中被修改了，那么就会抛出ConcurrentModificationException异常。
readObject()</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span><span class="lnt">16
</span><span class="lnt">17
</span><span class="lnt">18
</span><span class="lnt">19
</span><span class="lnt">20
</span><span class="lnt">21
</span><span class="lnt">22
</span><span class="lnt">23
</span><span class="lnt">24
</span><span class="lnt">25
</span><span class="lnt">26
</span><span class="lnt">27
</span><span class="lnt">28
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-fallback" data-lang="fallback"><span class="line"><span class="cl">/**
</span></span><span class="line"><span class="cl"> * 私有的方法
</span></span><span class="line"><span class="cl"> * 以反序列化中重构ArrayList实例
</span></span><span class="line"><span class="cl"> * Reconstitute the &lt;tt&gt;ArrayList&lt;/tt&gt; instance from a stream (that is,
</span></span><span class="line"><span class="cl"> * deserialize it).
</span></span><span class="line"><span class="cl"> */
</span></span><span class="line"><span class="cl">private void readObject(java.io.ObjectInputStream s)
</span></span><span class="line"><span class="cl">        throws java.io.IOException, ClassNotFoundException {
</span></span><span class="line"><span class="cl">    elementData = EMPTY_ELEMENTDATA;
</span></span><span class="line"><span class="cl">    // 读出大小和隐藏的东西
</span></span><span class="line"><span class="cl">    // Read in size, and any hidden stuff
</span></span><span class="line"><span class="cl">    s.defaultReadObject();
</span></span><span class="line"><span class="cl">    // Read in capacity
</span></span><span class="line"><span class="cl">    //　读出大小
</span></span><span class="line"><span class="cl">    s.readInt(); // ignored
</span></span><span class="line"><span class="cl">    if (size &gt; 0) {
</span></span><span class="line"><span class="cl">        // be like clone(), allocate array based upon size not capacity
</span></span><span class="line"><span class="cl">        int capacity = calculateCapacity(elementData, size);
</span></span><span class="line"><span class="cl">        SharedSecrets.getJavaOISAccess().checkArray(s, Object[].class, capacity);
</span></span><span class="line"><span class="cl">        ensureCapacityInternal(size);
</span></span><span class="line"><span class="cl">        Object[] a = elementData;
</span></span><span class="line"><span class="cl">        // Read in all elements in the proper order.
</span></span><span class="line"><span class="cl">        // 从输入流将“所有元素读出”
</span></span><span class="line"><span class="cl">        for (int i = 0; i &lt; size; i++) {
</span></span><span class="line"><span class="cl">            a[i] = s.readObject();
</span></span><span class="line"><span class="cl">        }
</span></span><span class="line"><span class="cl">    }
</span></span><span class="line"><span class="cl">}
</span></span></code></pre></td></tr></table>
</div>
</div><p>readObject()方法将那么没有参与序列化的属性读取出来。</p>
<h5 id="14-arraylist中还有一个clone方法">14. ArrayList中还有一个clone()方法</h5>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span><span class="lnt">16
</span><span class="lnt">17
</span><span class="lnt">18
</span><span class="lnt">19
</span><span class="lnt">20
</span><span class="lnt">21
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-fallback" data-lang="fallback"><span class="line"><span class="cl">/**
</span></span><span class="line"><span class="cl"> * 实现Cloneable接口，深度复制
</span></span><span class="line"><span class="cl"> * Returns a shallow copy of this &lt;tt&gt;ArrayList&lt;/tt&gt; instance. (Th
</span></span><span class="line"><span class="cl"> * elements themselves are not copied.)
</span></span><span class="line"><span class="cl"> *
</span></span><span class="line"><span class="cl"> * @return a clone of this &lt;tt&gt;ArrayList&lt;/tt&gt; instance
</span></span><span class="line"><span class="cl"> */
</span></span><span class="line"><span class="cl">public Object clone() {
</span></span><span class="line"><span class="cl">    try {
</span></span><span class="line"><span class="cl">        // Object的克隆会复制本对象的其内的基本类型和String类型，但不会复制对象
</span></span><span class="line"><span class="cl">        ArrayList&lt;?&gt; v = (ArrayList&lt;?&gt;) super.clone();
</span></span><span class="line"><span class="cl">        // 将elemetData进行复制
</span></span><span class="line"><span class="cl">        v.elementData = Arrays.copyOf(elementData, size);
</span></span><span class="line"><span class="cl">        // 因为复制出来的是新的，没有被修改过，所以modCount初始化为0
</span></span><span class="line"><span class="cl">        v.modCount = 0;
</span></span><span class="line"><span class="cl">        return v;
</span></span><span class="line"><span class="cl">    } catch (CloneNotSupportedException e) {
</span></span><span class="line"><span class="cl">        // this shouldn&#39;t happen, since we are Cloneable
</span></span><span class="line"><span class="cl">        throw new InternalError(e);
</span></span><span class="line"><span class="cl">    }
</span></span><span class="line"><span class="cl">}
</span></span></code></pre></td></tr></table>
</div>
</div><p><strong>剩余的方法还是比较多的但是都比较好理解，与数组中元素移动相关的都使用到了System.arraycopy()这个方法</strong>。所以在这里就不多讲了。
下面将一些比较重要的方法</p>
</div><div class="post-footer" id="post-footer">
    <div class="post-info">
        <div class="post-info-line">
            <div class="post-info-mod">
                <span>更新于 2018-05-27</span>
            </div></div>
        <div class="post-info-line">
            <div class="post-info-md"><span>
                            <a class="link-to-markdown" href="/arraylist%E6%BA%90%E7%A0%81%E9%98%85%E8%AF%BB%E4%B8%8A/index.md" target="_blank">阅读原始文档</a>
                        </span></div>
            <div class="post-info-share">
                <span><a href="javascript:void(0);" title="分享到 Twitter" data-sharer="twitter" data-url="http://example.org/arraylist%E6%BA%90%E7%A0%81%E9%98%85%E8%AF%BB%E4%B8%8A/" data-title="ArrayList源码阅读（上）" data-hashtags="Java"><i class="fab fa-twitter fa-fw" aria-hidden="true"></i></a><a href="javascript:void(0);" title="分享到 Facebook" data-sharer="facebook" data-url="http://example.org/arraylist%E6%BA%90%E7%A0%81%E9%98%85%E8%AF%BB%E4%B8%8A/" data-hashtag="Java"><i class="fab fa-facebook-square fa-fw" aria-hidden="true"></i></a><a href="javascript:void(0);" title="分享到 Hacker News" data-sharer="hackernews" data-url="http://example.org/arraylist%E6%BA%90%E7%A0%81%E9%98%85%E8%AF%BB%E4%B8%8A/" data-title="ArrayList源码阅读（上）"><i class="fab fa-hacker-news fa-fw" aria-hidden="true"></i></a><a href="javascript:void(0);" title="分享到 Line" data-sharer="line" data-url="http://example.org/arraylist%E6%BA%90%E7%A0%81%E9%98%85%E8%AF%BB%E4%B8%8A/" data-title="ArrayList源码阅读（上）"><i data-svg-src="https://cdn.jsdelivr.net/npm/simple-icons@7.3.0/icons/line.svg" aria-hidden="true"></i></a><a href="javascript:void(0);" title="分享到 微博" data-sharer="weibo" data-url="http://example.org/arraylist%E6%BA%90%E7%A0%81%E9%98%85%E8%AF%BB%E4%B8%8A/" data-title="ArrayList源码阅读（上）" data-ralateuid="xxxx"><i class="fab fa-weibo fa-fw" aria-hidden="true"></i></a></span>
            </div>
        </div>
    </div>

    <div class="post-info-more">
        <section class="post-tags"><i class="fas fa-tags fa-fw" aria-hidden="true"></i>&nbsp;<a href="/tags/java/">Java</a></section>
        <section>
            <span><a href="javascript:void(0);" onclick="window.history.back();">返回</a></span>&nbsp;|&nbsp;<span><a href="/">主页</a></span>
        </section>
    </div>

    <div class="post-nav"><a href="/%E9%93%BE%E8%A1%A8/" class="prev" rel="prev" title="链表"><i class="fas fa-angle-left fa-fw" aria-hidden="true"></i>链表</a>
            <a href="/arraylist-%E6%BA%90%E7%A0%81%E9%98%85%E8%AF%BB%E4%B8%AD/" class="next" rel="next" title="ArrayList源码阅读（中）——迭代器和子集合">ArrayList源码阅读（中）——迭代器和子集合<i class="fas fa-angle-right fa-fw" aria-hidden="true"></i></a></div>
</div>
<div id="comments"><div id="valine" class="comment"></div><noscript>
                Please enable JavaScript to view the comments powered by <a href="https://valine.js.org/">Valine</a>.
            </noscript></div></article></div>
            </main><footer class="footer">
        <div class="footer-container"><div class="footer-line">由 <a href="https://gohugo.io/" target="_blank" rel="noopener noreffer" title="Hugo 0.109.0">Hugo</a> 强力驱动 | 主题 - <a href="https://github.com/dillonzq/LoveIt" target="_blank" rel="noopener noreffer" title="LoveIt 0.2.11"><i class="far fa-kiss-wink-heart fa-fw" aria-hidden="true"></i> LoveIt</a>
                </div><div class="footer-line" itemscope itemtype="http://schema.org/CreativeWork"><i class="far fa-copyright fa-fw" aria-hidden="true"></i><span itemprop="copyrightYear">2023</span><span class="author" itemprop="copyrightHolder">&nbsp;<a href="/" target="_blank">MC</a></span>&nbsp;|&nbsp;<span class="license"><a rel="license external nofollow noopener noreffer" href="https://creativecommons.org/licenses/by-nc/4.0/" target="_blank">CC BY-NC 4.0</a></span></div>
        </div>
    </footer></div>

        <div id="fixed-buttons"><a href="#" id="back-to-top" class="fixed-button" title="回到顶部">
                <i class="fas fa-arrow-up fa-fw" aria-hidden="true"></i>
            </a><a href="#" id="view-comments" class="fixed-button" title="查看评论">
                <i class="fas fa-comment fa-fw" aria-hidden="true"></i>
            </a>
        </div><link rel="stylesheet" href="/lib/valine/valine.min.css"><script type="text/javascript" src="https://cdn.jsdelivr.net/npm/valine@1.5.0/dist/Valine.min.js"></script><script type="text/javascript" src="https://cdn.jsdelivr.net/npm/autocomplete.js@0.38.1/dist/autocomplete.min.js"></script><script type="text/javascript" src="https://cdn.jsdelivr.net/npm/lunr@2.3.9/lunr.min.js"></script><script type="text/javascript" src="/lib/lunr/lunr.stemmer.support.min.js"></script><script type="text/javascript" src="/lib/lunr/lunr.zh.min.js"></script><script type="text/javascript" src="https://cdn.jsdelivr.net/npm/lazysizes@5.3.2/lazysizes.min.js"></script><script type="text/javascript" src="https://cdn.jsdelivr.net/npm/clipboard@2.0.11/dist/clipboard.min.js"></script><script type="text/javascript" src="https://cdn.jsdelivr.net/npm/sharer.js@0.5.1/sharer.min.js"></script><script type="text/javascript">window.config={"code":{"copyTitle":"复制到剪贴板","maxShownLines":50},"comment":{"valine":{"appId":"QGzwQXOqs5JOhN4RGPOkR2mR-MdYXbMMI","appKey":"WBmoGyJtbqUswvfLh6L8iEBr","avatar":"mp","el":"#valine","emojiCDN":"https://cdn.jsdelivr.net/npm/emoji-datasource-google@14.0.0/img/google/64/","emojiMaps":{"100":"1f4af.png","alien":"1f47d.png","anger":"1f4a2.png","angry":"1f620.png","anguished":"1f627.png","astonished":"1f632.png","black_heart":"1f5a4.png","blue_heart":"1f499.png","blush":"1f60a.png","bomb":"1f4a3.png","boom":"1f4a5.png","broken_heart":"1f494.png","brown_heart":"1f90e.png","clown_face":"1f921.png","cold_face":"1f976.png","cold_sweat":"1f630.png","confounded":"1f616.png","confused":"1f615.png","cry":"1f622.png","crying_cat_face":"1f63f.png","cupid":"1f498.png","dash":"1f4a8.png","disappointed":"1f61e.png","disappointed_relieved":"1f625.png","dizzy":"1f4ab.png","dizzy_face":"1f635.png","drooling_face":"1f924.png","exploding_head":"1f92f.png","expressionless":"1f611.png","face_vomiting":"1f92e.png","face_with_cowboy_hat":"1f920.png","face_with_hand_over_mouth":"1f92d.png","face_with_head_bandage":"1f915.png","face_with_monocle":"1f9d0.png","face_with_raised_eyebrow":"1f928.png","face_with_rolling_eyes":"1f644.png","face_with_symbols_on_mouth":"1f92c.png","face_with_thermometer":"1f912.png","fearful":"1f628.png","flushed":"1f633.png","frowning":"1f626.png","ghost":"1f47b.png","gift_heart":"1f49d.png","green_heart":"1f49a.png","grimacing":"1f62c.png","grin":"1f601.png","grinning":"1f600.png","hankey":"1f4a9.png","hear_no_evil":"1f649.png","heart":"2764-fe0f.png","heart_decoration":"1f49f.png","heart_eyes":"1f60d.png","heart_eyes_cat":"1f63b.png","heartbeat":"1f493.png","heartpulse":"1f497.png","heavy_heart_exclamation_mark_ornament":"2763-fe0f.png","hole":"1f573-fe0f.png","hot_face":"1f975.png","hugging_face":"1f917.png","hushed":"1f62f.png","imp":"1f47f.png","innocent":"1f607.png","japanese_goblin":"1f47a.png","japanese_ogre":"1f479.png","joy":"1f602.png","joy_cat":"1f639.png","kiss":"1f48b.png","kissing":"1f617.png","kissing_cat":"1f63d.png","kissing_closed_eyes":"1f61a.png","kissing_heart":"1f618.png","kissing_smiling_eyes":"1f619.png","laughing":"1f606.png","left_speech_bubble":"1f5e8-fe0f.png","love_letter":"1f48c.png","lying_face":"1f925.png","mask":"1f637.png","money_mouth_face":"1f911.png","nauseated_face":"1f922.png","nerd_face":"1f913.png","neutral_face":"1f610.png","no_mouth":"1f636.png","open_mouth":"1f62e.png","orange_heart":"1f9e1.png","partying_face":"1f973.png","pensive":"1f614.png","persevere":"1f623.png","pleading_face":"1f97a.png","pouting_cat":"1f63e.png","purple_heart":"1f49c.png","rage":"1f621.png","relaxed":"263a-fe0f.png","relieved":"1f60c.png","revolving_hearts":"1f49e.png","right_anger_bubble":"1f5ef-fe0f.png","robot_face":"1f916.png","rolling_on_the_floor_laughing":"1f923.png","scream":"1f631.png","scream_cat":"1f640.png","see_no_evil":"1f648.png","shushing_face":"1f92b.png","skull":"1f480.png","skull_and_crossbones":"2620-fe0f.png","sleeping":"1f634.png","sleepy":"1f62a.png","slightly_frowning_face":"1f641.png","slightly_smiling_face":"1f642.png","smile":"1f604.png","smile_cat":"1f638.png","smiley":"1f603.png","smiley_cat":"1f63a.png","smiling_face_with_3_hearts":"1f970.png","smiling_imp":"1f608.png","smirk":"1f60f.png","smirk_cat":"1f63c.png","sneezing_face":"1f927.png","sob":"1f62d.png","space_invader":"1f47e.png","sparkling_heart":"1f496.png","speak_no_evil":"1f64a.png","speech_balloon":"1f4ac.png","star-struck":"1f929.png","stuck_out_tongue":"1f61b.png","stuck_out_tongue_closed_eyes":"1f61d.png","stuck_out_tongue_winking_eye":"1f61c.png","sunglasses":"1f60e.png","sweat":"1f613.png","sweat_drops":"1f4a6.png","sweat_smile":"1f605.png","thinking_face":"1f914.png","thought_balloon":"1f4ad.png","tired_face":"1f62b.png","triumph":"1f624.png","two_hearts":"1f495.png","unamused":"1f612.png","upside_down_face":"1f643.png","weary":"1f629.png","white_frowning_face":"2639-fe0f.png","white_heart":"1f90d.png","wink":"1f609.png","woozy_face":"1f974.png","worried":"1f61f.png","yawning_face":"1f971.png","yellow_heart":"1f49b.png","yum":"1f60b.png","zany_face":"1f92a.png","zipper_mouth_face":"1f910.png","zzz":"1f4a4.png"},"enableQQ":false,"highlight":true,"lang":"zh-CN","pageSize":10,"placeholder":"你的评论 ...","recordIP":true,"serverURLs":"https://leancloud.hugoloveit.com","visitor":true}},"search":{"highlightTag":"em","lunrIndexURL":"/index.json","lunrLanguageCode":"zh","lunrSegmentitURL":"/lib/lunr/lunr.segmentit.js","maxResultLength":10,"noResultsFound":"没有找到结果","snippetLength":50,"type":"lunr"}};</script><script type="text/javascript" src="/js/theme.min.js"></script></body>
</html>
